package dbtools.structures;

import java.io.Serializable;

import dbtools.DBTransaction;

public abstract class DBField<T> implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private boolean isDescending = false;
	
	T value=null;
	String name = null;

	private DBTable table;
	
	public void beforeInsert(DBTransaction transaction){
		
	}
	
	public void beforeUpdate(DBTransaction transaction){
		
	}
	
	public String getName() {
		return name;
	}

	public void _setName(String name) {
		this.name = name;
	}

	public T getValue(){
		return value;
	}
	
	public void setValue(T value){
		this.value = value;
	}
	
	public Object getValueForSQL(){
		return value;
	}
	
	public void _setValueFromSQL(Object value){
		this.value = (T) value;
	}
	
	public abstract String getSqlType();

	public void _setTable(DBTable table) {
		this.table = table;
	}
	
	/**
	 * Returns the table in which this field is declared.
	 * @return
	 */
	public DBTable getTable(){
		return table;
	}
	
	public DBCond c_eq(DBField<T> other){
		return new DBCond(this,DBCond.EQUALS,other);
	}
	
	public DBCond c_eq(T value){
		return new DBCond(this,DBCond.EQUALS,value);
	}
	
	public DBCond c_gt(DBField<T> other){
		return new DBCond(this,DBCond.GT,other);
	}
	
	public DBCond c_gt(T value){
		return new DBCond(this,DBCond.GT,value);
	}
	
	public DBCond c_ge(DBField<T> other){
		return new DBCond(this,DBCond.GE,other);
	}
	
	public DBCond c_ge(T value){
		return new DBCond(this,DBCond.GE,value);
	}

	public DBField<T> desc(){
		DBField<T> descF = new DBField<T>() {
			@Override
			public String getSqlType() {
				return getSqlType();
			}
		};
		descF._setName(getName());
		descF._setTable(table);
		descF.isDescending=true;
		return descF;
	}
	
	public boolean isDescending() {
		return isDescending;
	}

}
